package edu.xjdx.os.service.Impl;

import edu.xjdx.os.domain.Dictionary;
import edu.xjdx.os.domain.Goods;
import edu.xjdx.os.mapper.DictionaryMapper;
import edu.xjdx.os.mapper.GoodsMapper;
import edu.xjdx.os.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Override
    public List<Goods> listAll() {
        return goodsMapper.findAll();
    }

    @Override
    public void remove(String id) {
        goodsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void modify(Goods goods) {
        goodsMapper.updateByPrimaryKey(goods);
    }

    @Override
    public void add(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public List<Goods> findByKeyword(String keyword,String orderType) {
        List<Goods> goodsList;
        switch (orderType){
            case "default":
                goodsList=goodsMapper.findByKeyword(keyword);
                break;
            case "popularity":
                goodsList=goodsMapper.keywordOrderBySold(keyword);
                break;
            case "price-asc":
                goodsList=goodsMapper.keywordOrderByPriceAsc(keyword);
                break;
            case "price-desc":
                goodsList=goodsMapper.keywordOrderByPriceDesc(keyword);
                break;
            default:
                goodsList=goodsMapper.findByKeyword(keyword);
        }
        return goodsList;
    }

    @Override
    public List<Goods> findByCategory(String keyword,String orderType) {
        List<Goods> goodsList;
        switch (orderType){
            case "default":
                goodsList=goodsMapper.findByCategory(keyword);
                break;
            case "popularity":
                goodsList=goodsMapper.categoryOrderBySold(keyword);
                break;
            case "price-asc":
                goodsList=goodsMapper.categoryOrderByPriceAsc(keyword);
                break;
            case "price-desc":
                goodsList=goodsMapper.categoryOrderByPriceDesc(keyword);
                break;
            default:
                goodsList=goodsMapper.findByCategory(keyword);
        }
        return goodsList;
    }

    @Override
    public List<Goods> orderList(String orderType) {
        List<Goods> goodsList;
        switch (orderType){
            case "default":
                goodsList=goodsMapper.findAll();
                break;
            case "popularity":
                goodsList=goodsMapper.orderBySold();
                break;
            case "price-asc":
                goodsList=goodsMapper.orderByPriceAsc();
                break;
            case "price-desc":
                goodsList=goodsMapper.orderByPriceDesc();
                break;
            default:
                goodsList=goodsMapper.findAll();
        }
        return goodsList;
    }

    @Override
    public Goods findById(String id) {
        return goodsMapper.selectByPrimaryKey(id);
    }
}
